import receiverMgr from '@/biz/receiver/receiver'

let _this = null

function getPhoneByCity(data) {
  let params = {
    ...data
  }
  console.log(params, 'params')
  if (params.city) {
    return receiverMgr.getPhoneByCity(params).then((res) => {
      res.list &&
        res.list.forEach((item) => {
          item.key = item.phoneX
          item.value = item.phoneX
        })
      return res
    })
  } else {
    return Promise.resolve([])
  }
}

function config(key1, key2, key3) {
  let arr = [
    {
      col: 12,
      title: '呼入号归属地',
      key: key1,
      load: (o) => {
        receiverMgr.getCityDictionaries().then((res) => {
          let arr = []
          res &&
            res.forEach((item) => {
              let obj = {}
              obj.key = item
              obj.value = item
              console.log(obj, 1111)
              arr.push(obj)
            })
          console.log(arr)
          o.select.options = arr
        })
      },
      select: {
        options: []
        // clearable: true
      },
      data: {
        from: (info) => {
          console.log(info, '***((((')
          return info[key1]
        }
      },
      change: () => {
        _this.info[key2] = null

        getPhoneByCity({ page: 1, rows: 10, city: _this.info[key1] }).then((res) => {
          arr[1].group[0].select.options = res.list
        })
      }
    },
    {
      col: 12,
      title: '呼入号码',
      key: key2,
      group: [
        {
          title: '',
          key: key2,
          load: (o) => {
            getPhoneByCity({ page: 1, rows: 10, city: _this.info[key1] }).then((res) => {
              if (_this.info[key2]) {
                let obj = { key: _this.info[key2], value: _this.info[key2] }
                res.list.unshift(obj)
              }
              o.select.options = res.list
            })
          },
          select: {
            options: []
          }
        },
        {
          title: '',
          button: {
            type: 'text',
            click: (data) => {
              if (!_this.info[key1]) return false
              _this.info[key2] = null
              ++_this.callInPage.page
              if (_this.callInPage.pages < _this.callInPage.page) {
                _this.callInPage.page = 1
              }
              let params = {
                page: _this.callInPage.page,
                rows: _this.callInPage.rows,
                city: _this.info[key1]
              }
              getPhoneByCity(params).then((res) => {
                _this.callInPage.pages = res.pages
                _this.$Message.success('已更换')
                arr[1].group[0].select.options = res.list
              })
            },
            style: {
              color: '#2d8cf0'
            },
            title: '换一批'
          }
        }
      ]
    }
  ]
  if (_this.info[key3]) {
    return arr
  } else {
    return []
  }
}

function options(vm) {
  _this = vm
  let arr = [
    {
      title: '案场',
      key: 'housesName',
      text: {}
    },
    {
      title: '',
      visible: _this.info.tospurVisible,
      key: 'tospurLabel',
      text: {
        fontSize: '15px',
        color: '#F59A23',
        fontWeight: 'bold'
      }
    },
    config('tcCity', 'tcInPhone', 'tospurVisible'),
    {
      title: '',
      visible: _this.info.baiduVisible,
      key: 'baiduLabel',
      text: {
        fontSize: '15px',
        color: '#F59A23',
        fontWeight: 'bold'
      }
    },
    config('bdCity', 'bdInPhone', 'baiduVisible')
  ]

  return arr
}

export default {
  options
}
